package com.z3n.entities.controller;

import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import com.z3n.entities.Z3nGroup;
import java.util.List;
import javax.persistence.Query;

@SuppressWarnings("unchecked")
@JPAManager(targetEntity = com.z3n.entities.Z3nGroup.class)
public class Z3nGroupManager {

	private EntityManagerFactory emf;

	public Z3nGroupManager() {
	
	}

	public Z3nGroupManager(EntityManagerFactory emf) {
		this.emf = emf;
	}

	public void setEntityManagerFactory(EntityManagerFactory emf) {
		this.emf = emf;
	}

	private EntityManager getEntityManager() {
		if (emf == null) {
			throw new RuntimeException(
					"The EntityManagerFactory is null.  This must be passed in to the constructor or set using the setEntityManagerFactory() method.");
		}
		return emf.createEntityManager();
	}

	@Action(Action.ACTION_TYPE.CREATE)
	public String createZ3nGroup(Z3nGroup Z3nGroup) throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(Z3nGroup);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.DELETE)
	public String deleteZ3nGroup(Z3nGroup Z3nGroup) throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nGroup = em.merge(Z3nGroup);
			em.remove(Z3nGroup);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.UPDATE)
	public String updateZ3nGroup(Z3nGroup Z3nGroup) throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nGroup = em.merge(Z3nGroup);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.FIND)
	public Z3nGroup findZ3nGroupByGroupId(int groupId) {
		Z3nGroup Z3nGroup = null;
		EntityManager em = getEntityManager();
		try {
			Z3nGroup = (Z3nGroup) em.find(Z3nGroup.class, groupId);
		} finally {
			em.close();
		}
		return Z3nGroup;
	}

	@Action(Action.ACTION_TYPE.NEW)
	public Z3nGroup getNewZ3nGroup() {
	
		Z3nGroup Z3nGroup = new Z3nGroup();
	
		return Z3nGroup;
	}

	@NamedQueryTarget("getZ3nGroupByGroupName")
	public List<Z3nGroup> getZ3nGroupByGroupName(String groupName) {
		EntityManager em = getEntityManager();
		List<Z3nGroup> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroupByGroupName");
			query.setParameter("groupName", groupName);
			results = (List<Z3nGroup>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nGroupByGroupId")
	public List<Z3nGroup> getZ3nGroupByGroupId(int groupId) {
		EntityManager em = getEntityManager();
		List<Z3nGroup> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroupByGroupId");
			query.setParameter("groupId", groupId);
			results = (List<Z3nGroup>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nGroup")
	public List<Z3nGroup> getZ3nGroup() {
		EntityManager em = getEntityManager();
		List<Z3nGroup> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroup");
			results = (List<Z3nGroup>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

}